package com.starhub.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.starhub.dao.ProductDao;
import com.starhub.dao.StarHubBaseDao;
import com.starhub.entity.ProductCarousel;

public class ProductDaoImpl extends StarHubBaseDao implements ProductDao {

	public List<ProductCarousel> getProductCarousels(int categoryId) {
		Session session = sessionFactory.openSession();
		return session.createQuery("from ProductCarousel PS where PS.category.id = :categoryId").setInteger("categoryId", categoryId).list();
	}

	public ProductCarousel getProducCarouselDetails(int productId) {
		Session session = sessionFactory.openSession();
		ProductCarousel productCarousel = (ProductCarousel) session.get(ProductCarousel.class, productId);
		return productCarousel;
	}

	public void updateProductCarousel(ProductCarousel productCarousel) {
		
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		ProductCarousel oldProduct = (ProductCarousel) session.get(ProductCarousel.class, productCarousel.getId());
		oldProduct.setBackgroundImage(productCarousel.getBackgroundImage());
		oldProduct.setBannerText(productCarousel.getBannerText());
		oldProduct.setBannerTitle(productCarousel.getBannerTitle());
		oldProduct.setLink(productCarousel.getLink());
		
		session.saveOrUpdate(oldProduct);
		tx.commit();
		session.close();
	}

}
